package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.rcy.ruicingmarket.cache.UmsCacheGroup;
import org.rcy.ruicingmarket.exception.UmsException;
import org.rcy.ruicingmarket.user.LoginOrRegisterMode;
import org.rcy.ruicingmarket.user.LoginVo;
import org.rcy.ruicingmarket.user.PasswordChangeVo;
import org.rcy.ruicingmarket.user.RegisterVo;
import org.rcy.ruicingmarket.user.UserEditVo;
import org.rcy.ruicingmarket.user.UserQueryCondition;
import org.rcy.ruicingmarket.user.UserVo;
import org.rcy.ruicingmarket.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author RuicyWu
 * @Date 2021/12/29 10:27
 */
@RestController
@RequestMapping("/user")
@Tag(name = "user", description = "前台用户控制器")
public class UserController {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    /**
     * 通过手机号或邮箱登录
     * @param vo
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录", tags = {"user"})
    public ResultMessage loginFront(@RequestBody LoginVo vo)
    {
        try {
            String token = userService.loginVerify(vo);
            return ResultMessage.success(token);
        }catch (UmsException e){
            logger.error(e.getMessage(),e);
            return  ResultMessage.error(e.getCode());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    /**
     * 前台用户注册
     * @param vo
     * @return
     */

    @PostMapping("/regist")
    @Operation(summary = "注册", description = "注册", tags = {"user"})
    public ResultMessage registFront(@RequestBody RegisterVo vo)
    {
        //判断是通过邮箱还是手机注册的
        try {
            String token = userService.registUser(vo);
            return ResultMessage.success(token);
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    /**
     * 分页查询
     * @param queryCondition
     * @return
     */
    @PostMapping("/querypage")
    @Operation(summary = "分页查询", description = "分页查询用户", tags = {"user"})
    public ResultMessage pageQueryUser(@RequestBody UserQueryCondition queryCondition)
    {
        try {
            return ResultMessage.success(userService.queryUserByCondition(queryCondition));
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }


    /**
     * 修改用户信息
     * @param vo
     * @return
     */
    @PutMapping("/edit")
    @Operation(summary = "修改用户", description = "修改用户信息", tags = {"user"})
    public ResultMessage editUser(@RequestBody UserEditVo vo)
    {
        try {
            userService.edit(vo);
            return ResultMessage.success();
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询用户", description = "根据ID查询用户", tags = {"user"})
    public ResultMessage<UserVo> getById(@PathVariable("id")Long id){
        try {
            return ResultMessage.success(userService.findById(id));
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    /**
     * 注销用户
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @Operation(summary = "注销", description = "用户注销", tags = {"user"})
    public ResultMessage removeUser(@PathVariable("id") Long id)
    {
        try {
            userService.remove(id);
            return ResultMessage.success();
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    /**
     * 冻结用户
     * @param id
     * @return
     */
    @PutMapping("/freeze/{id}")
    @Operation(summary = "冻结用户", description = "冻结用户", tags = {"user"})
    public ResultMessage freezeUser(@PathVariable("id") Long id)
    {
           try {
               userService.freezeUser(id);
               return ResultMessage.success();
           }catch (Exception e)
           {
               logger.error(e.getMessage(),e);
               return ResultMessage.error();
           }
    }

    @Operation(summary = "修改密码", description = "修改密码", tags = {"user"})
    @PutMapping("/updatepass")
    public ResultMessage updatePassword(@RequestBody PasswordChangeVo vo){
        try {

            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PostMapping("/sendcode")
    @Operation(summary = "发送验证码", description = "发送验证码", tags = {"user"})
    public ResultMessage sendCode(String mode,String receiver,String action){
        try {
            if("login".equals(action)){
                userService.sendLoginOrRegisterCode(receiver, LoginOrRegisterMode.valueOf(mode), UmsCacheGroup.LOGINCODE);
            }else {
                userService.sendLoginOrRegisterCode(receiver, LoginOrRegisterMode.valueOf(mode), UmsCacheGroup.REGISTERCODE);
            }
            return ResultMessage.success();
        }catch (Exception e)
        {
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }
}
